using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace MimeCloud.Foundations.Extensions
{
    public static class StringExtensions
    {
        private static readonly Regex CommaSeparationRegex = new Regex(@"\s*,\s*");
        
        public static string NullifyEmpty(this string s, bool treatWhitespaceAsEmpty)
        {
            if (s == null)
            {
                return s;
            }
            string checkString = treatWhitespaceAsEmpty ? s.Trim() : s;
            return string.IsNullOrEmpty(checkString) ? null : s;       
        }

        public static string NullifyEmpty(this string s)
        {
            return NullifyEmpty(s, true);
        }
        
        public static string UrlEncode(this string s)
        {
            return HttpUtility.UrlEncode(s);
        }
        
        public static string HtmlEncode(this string s)
        {
            return HttpUtility.HtmlEncode(s);
        }
        
        /// <summary>
        /// Enums the parse.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static T EnumParse<T>(this string value)
        {
            return (T) Enum.Parse(typeof (T), value);
        }

        /// <summary>
        /// Fills the specified string to fill.
        /// </summary>
        /// <param name="stringToFill">The string to fill.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static string Fill(this string stringToFill, params object[] parameters)
        {
            string stringToFillReference = stringToFill;
            return string.Format(stringToFillReference, parameters);
        }

        /// <summary>
        /// strin to int.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static int ToInt(this string value)
        {
            try
            {
                return int.Parse(value);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    
        /// <summary>
        /// Remove all characters in the string,
        /// after and including the last occurrence of 'c'
        /// </summary>
        /// <param name="s"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string RemoveAfter(this string s, char c)
        {
            int lastIndex = s.LastIndexOf(c);
            return (lastIndex >= 0) ? s.Remove(lastIndex) : s;
        }
    
        public static IList<string> SplitCommaSeparated(this string s)
        {
            return CommaSeparationRegex.Split(s ?? "").Where(t => !string.IsNullOrEmpty(t)).ToList();
        }
        
    }
}